import pymongo ;
import os ;
import re ;
import datetime;
from pymongo import MongoClient;
import time
from PyQt4.QtCore import QDir,QObject,QThread,pyqtSignal
from KDataBase import KGlobals
from KDataBase.KDBUtility import KDBUtility
from KDataBase.KDBProvider import KEODProvider,KStooqProvider

class KAbstractDB(QObject):
	"""The base class of KBuildDB,KUpdateDB,KQueryDB and So on.
	"""
	## signals 
	ProgressUpdated=pyqtSignal(str)
	finished=pyqtSignal()
	def UpdateProgress(self,text):
		self.ProgressUpdated.emit(str(text))
		print(text)
	## thread related
	@property
	def ThreadArgs(self):
		return self._threadargs
	@ThreadArgs.setter
	def ThreadArgs(self,value):
		self._threadargs=value
	@property
	def Stop(self):
		if not hasattr(self,'stopped'):
			self.stopped=False
		return self.stopped
	@Stop.setter
	def Stop(self,value):
		self.stopped=value 
		if hasattr(self,'provider') and self.provider:
			self.provider.Stop=True
	## general database functions
	def __init__(self,parent=None):
		QObject.__init__(self)
		self.client=MongoClient()
		self.db=self.client[KGlobals.DatabaseName]
	## execute command in new thread
	def ExectueCommandInThread(self,para):

		self.workthread = QThread() ## execut in seperate thread
		self.Stop=False
		self.ThreadArgs=para
		self.moveToThread(self.workthread)
		self.workthread.finished.connect(self.workthread.deleteLater)
		self.workthread.started.connect(self.DoWork)

		self.finished.connect(self.workthread.quit)
		self.finished.connect(self.deleteLater)    
		self.workthread.start()
	def DoWork(self): ##called by new thread
		pass










